import COS from './cos-wx-sdk-v5.js'

let cos = new COS({
     SecretId: 'AKIDe4JJvInM6bssefr3LOAgvwGLBk27jZ4j',
     SecretKey: 'ZlgJkrMa6LOAeBSTDgofKpxkFbcQecJn',
})
cos.token = {
	Bucket: 'yzr2020-1258429991',
	Region: 'ap-nanjing',
}
cos.path = 'media/'

cos.formatDateTime =  function(fmt) {
	if(!fmt){
		fmt = "yyyy-MM-dd hh:mm:ss"
	}
	let datetime = new Date();
	var o = {
		"M+": datetime.getMonth() + 1, //月份
		"d+": datetime.getDate(), //日
		"h+": datetime.getHours(), //小时
		"m+": datetime.getMinutes(), //分
		"s+": datetime.getSeconds(), //秒
		"q+": Math.floor((datetime.getMonth() + 3) / 3), //季度
		"S": datetime.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (datetime.getFullYear() + "").substr(4 - RegExp.$1
		.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length ==
			1) ? (o[k]) : (("00" +
			o[k]).substr(("" + o[k]).length)));
	return fmt;
}

cos.uuidStr = function() {
	function S4() {
		return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
	}
	//return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
	return (S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4());
}

cos.createKey = function(){
	return  cos.path + cos.formatDateTime("yyyyMMdd") + "/" + cos.uuidStr()
}

cos.error = function(err){  //错误处理
	uni.showToast({
		title: err.error,
	})
	console.log(err.error);
}

/*{"Location":"yzr2020-1258429991.cos.ap-nanjing.myqcloud.com/bbb",
"ETag":"\"41fcba09f2bdcdf315ba4119dc7978dd\"","statusCode":200,
"headers":{"Content-Length":"0","Connection":"keep-alive","Date":"Fri, 16 Oct 2020 13:41:33 GMT",
"ETag":"\"41fcba09f2bdcdf315ba4119dc7978dd\"","Server":"tencent-cos","x-cos-hash-crc64ecma":"302133438406913998",
"x-cos-request-id":"NWY4OWEzMGRfMjcyZDZkMDlfNWRlOTVfOTcyN2U3",
"x-cos-trace-id":"OGVmYzZiMmQzYjA2OWNhODk0NTRkMTBiOWVmMDAxODc0OWRkZjk0ZDM1NmI1M2E2MTRlY2MzZDhmNmI5MWI1OWE4OGMxZjNjY2JiNTBmMTVmMWY1MzAzYzkyZGQ2ZWM4MWQ4ODlmYjkwZjkwNTFhOGY4MjFmNDQzMWQ3MThmNjg="}}*/
cos.put = function(param, action){
	let key = cos.createKey()
	cos.putObject({
	    ...cos.token,
		...param,		
	    Key: key,
	    Body: param.Body,
	}, function(err, data) {
		if (err) {
			cos.error(err)
			//console.log(err.error);
		}else{
			if(action){
				data.key = key
				action(data)
			}
		}
	});
}

/*
{"Location":"yzr2020-1258429991.cos.ap-nanjing.myqcloud.com/img",
"statusCode":200,
"headers":{"Content-Length":"0","Connection":"close","Date":"Fri, 16 Oct 2020 13:53:24 GMT","ETag":"\"5aaa5bab773745e3c8ad1afadca901e7\"","Location":"http://yzr2020-1258429991.cos.ap-nanjing.myqcloud.com/img","Server":"tencent-cos","x-cos-hash-crc64ecma":"2782910386107615799","x-cos-request-id":"NWY4OWE1ZDRfOTIzMzZkMDlfYWRiMl85YWNlODU="},"ETag":"\"5aaa5bab773745e3c8ad1afadca901e7\""}
*/
/*
loaded: 0
total: undefined
speed: 0
percent: 0
*/
cos.post = function(param, action, actionInfo){
	let key = cos.createKey()
	cos.postObject({
	    ...cos.token,
		...param,
	    Key: key,
	    FilePath: param.FilePath, // wx.chooseImage 选择文件得到的 tmpFilePath
	    onProgress: function(info) {
			//console.log(JSON.stringify(info));
			if(actionInfo){
				actionInfo(info)
			}
	    }
	}, function(err, data) {
		if (err) {
			cos.error(err)
			//console.log(err.error);
		}else{
			if(action){
				data.key = key
				action(data)
			}
		}
	});
}


/*
{"statusCode":204,
"headers":{"Connection":"keep-alive","Date":"Fri, 16 Oct 2020 13:54:37 GMT","Server":"tencent-cos","x-cos-request-id":"NWY4OWE2MWRfYTgyZjZkMDlfMTliOF81MzI4Yzc=","x-cos-trace-id":"OGVmYzZiMmQzYjA2OWNhODk0NTRkMTBiOWVmMDAxODc0OWRkZjk0ZDM1NmI1M2E2MTRlY2MzZDhmNmI5MWI1OWE4OGMxZjNjY2JiNTBmMTVmMWY1MzAzYzkyZGQ2ZWM4MWQ4ODlmYjkwZjkwNTFhOGY4MjFmNDQzMWQ3MThmNjg="}}
*/
cos.delete = function(param, action){
	cos.deleteObject({
	    ...cos.token,
		...param,
	    Key: param.Key,
	}, function(err, data) {
		if (err) {
			cos.error(err)
			//console.log(err.error);
		}else{
			if(action){
				action(data)
			}
		}
	});
}

cos.get = function(param, action){
	cos.getObject({
	    ...cos.token,
		...param,
	    Key: param.Key,
	}, function(err, data) {
		if (err) {
			cos.error(err)
			//console.log(err.error);
		}else{
			if(action){
				action(data)
			}
		}
	});
}
	

module.exports = cos





			//  this.cos.putObject({
			//     Bucket: 'yzr2020-1258429991', /* 必须 */
			//     Region: 'ap-nanjing',    /* 必须 */
			//     Key: 'bbb',              /* 必须 */
			//     Body: 'cccc',
			// }, function(err, data) {
			//     console.log(err || data);
			// });

			// this.cos.getObject({
			//     Bucket: 'yzr2020-1258429991', /* 必须 */
			//     Region: 'ap-nanjing',    /* 必须 */
			//       Key: 'bbb',              /* 必须 */
			//   }, function(err, data) {
			//       console.log(err || data);
			//   });


			// uni.chooseMedia({
			// 	count: 1,
			// 	maxDuration: 60,
			// 	sizeType: ['compressed'],
			// 	success: (res) => {


			// 		this.cos.postObject({
			// 			Bucket: 'yzr2020-1258429991',
			// 			/* 必须 */
			// 			Region: 'ap-nanjing',
			// 			/* 必须 */
			// 			Key: 'img',
			// 			FilePath: res.tempFiles[0].tempFilePath, // wx.chooseImage 选择文件得到的 tmpFilePath
			// 			onProgress: function(info) {
			// 				console.log(JSON.stringify(info));
			// 			}
			// 		}, function(err, data) {
			// 			console.log(err || data);
			// 		});

			// 	}
			// })


			// this.cos.getObject({
			// 	Bucket: 'yzr2020-1258429991',
			// 	Region: 'ap-nanjing',
			// 	Key: 'img'
			// }, function(err, data) {
			// 	console.log(err || data);
			// });
			
			// this.cos.deleteObject({
			// 	Bucket: 'yzr2020-1258429991',
			// 	Region: 'ap-nanjing',
			// 	Key: 'bbb'
			// }, function(err, data) {
			// 	console.log(err || data);
			// });